package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallGoods;
import org.linlinjava.litemall.db.domain.LitemallGoods.Column;
import org.linlinjava.litemall.db.mapper.LitemallGoodsDao;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LitemallGoodsService {
    Column[] columns = new Column[]{Column.id, Column.name, Column.brief, Column.picUrl, Column.isHot, Column.isNew, Column.counterPrice, Column.retailPrice};
    @Resource
    private LitemallGoodsDao goodsDao;

    /**
     * 获取热卖商品
     *
     * @param offset
     * @param limit
     * @return
     */
    public List<LitemallGoods> queryByHot(int offset, int limit) {
    	
    	
    	LambdaQuery<LitemallGoods> query = goodsDao.createLambdaQuery();
    	query.andEq(LitemallGoods::getIsHot, true);
    	query.andEq(LitemallGoods::getIsOnSale, true);
    	query.andEq(LitemallGoods::getDeleted, false);
    	query.orderBy("add_time desc");
    	query.limit(offset, limit);
    	return query.select();
		/*
		 * LitemallGoodsExample example = new LitemallGoodsExample();
		 * example.or().andIsHotEqualTo(true).andIsOnSaleEqualTo(true).andDeletedEqualTo
		 * (false); example.setOrderByClause("add_time desc");
		 * PageHelper.startPage(offset, limit);
		 * 
		 * return goodsMapper.selectByExampleSelective(example, columns);
		 */
    }

    /**
     * 获取新品上市
     *
     * @param offset
     * @param limit
     * @return
     */
    public List<LitemallGoods> queryByNew(int offset, int limit) {
    	
    	LambdaQuery<LitemallGoods> query = goodsDao.createLambdaQuery();
    	query.andEq(LitemallGoods::getIsNew, true);
    	query.andEq(LitemallGoods::getIsOnSale, true);
    	query.andEq(LitemallGoods::getDeleted, false);
    	query.orderBy("add_time desc");
    	query.limit(offset, limit);
    	return query.select();
    	
    	
		/*
		 * LitemallGoodsExample example = new LitemallGoodsExample();
		 * example.or().andIsNewEqualTo(true).andIsOnSaleEqualTo(true).andDeletedEqualTo
		 * (false); example.setOrderByClause("add_time desc");
		 * PageHelper.startPage(offset, limit);
		 * 
		 * return goodsMapper.selectByExampleSelective(example, columns);
		 */
    }

    /**
     * 获取分类下的商品
     *
     * @param catList
     * @param offset
     * @param limit
     * @return
     */
    public List<LitemallGoods> queryByCategory(List<Integer> catList, int offset, int limit) {
    	
    	LambdaQuery<LitemallGoods> query = goodsDao.createLambdaQuery();
    	query.andIn(LitemallGoods::getCategoryId, catList);
    	query.andEq(LitemallGoods::getIsOnSale, true);
    	query.andEq(LitemallGoods::getDeleted, false);
    	query.orderBy("add_time desc");
    	query.limit(offset, limit);
    	return query.select();
    	
    	
		/*
		 * LitemallGoodsExample example = new LitemallGoodsExample();
		 * example.or().andCategoryIdIn(catList).andIsOnSaleEqualTo(true).
		 * andDeletedEqualTo(false); example.setOrderByClause("add_time  desc");
		 * PageHelper.startPage(offset, limit);
		 * 
		 * return goodsMapper.selectByExampleSelective(example, columns);
		 */
    }


    /**
     * 获取分类下的商品
     *
     * @param catId
     * @param offset
     * @param limit
     * @return
     */
    public List<LitemallGoods> queryByCategory(Integer catId, int offset, int limit) {
    	
    	
    	LambdaQuery<LitemallGoods> query = goodsDao.createLambdaQuery();
    	query.andEq(LitemallGoods::getCategoryId, catId);
    	query.andEq(LitemallGoods::getIsOnSale, true);
    	query.andEq(LitemallGoods::getDeleted, false);
    	query.orderBy("add_time desc");
    	query.limit(offset, limit);
    	return query.select();
		/*
		 * LitemallGoodsExample example = new LitemallGoodsExample();
		 * example.or().andCategoryIdEqualTo(catId).andIsOnSaleEqualTo(true).
		 * andDeletedEqualTo(false); example.setOrderByClause("add_time desc");
		 * PageHelper.startPage(offset, limit);
		 * 
		 * return goodsMapper.selectByExampleSelective(example, columns);
		 */
    }


    public List<LitemallGoods> querySelective(Integer catId, Integer brandId, String keywords, Boolean isHot, Boolean isNew, Integer offset, Integer limit, String sort, String order) {
    	
    	LambdaQuery<LitemallGoods> query = goodsDao.createLambdaQuery();
    	
    	LambdaQuery<LitemallGoods> query1 = goodsDao.createLambdaQuery();
    	
    	LambdaQuery<LitemallGoods> query2 = goodsDao.createLambdaQuery();
    	
    	
    	if (!StringUtils.isEmpty(catId) && catId != 0) {
    		query1.andEq(LitemallGoods::getCategoryId, catId);
    		query2.andEq(LitemallGoods::getCategoryId, catId);
        }
        if (!StringUtils.isEmpty(brandId)) {
        	query1.andEq(LitemallGoods::getBrandId, brandId);
        	query2.andEq(LitemallGoods::getBrandId, brandId);
        }
        if (!StringUtils.isEmpty(isNew)) {
        	query1.andEq(LitemallGoods::getIsNew, isNew);
        	query2.andEq(LitemallGoods::getIsNew, isNew);
        	
        }
        if (!StringUtils.isEmpty(isHot)) {
        	query1.andEq(LitemallGoods::getIsHot, isHot);
        	query2.andEq(LitemallGoods::getIsHot, isHot);
        	
        }
        if (!StringUtils.isEmpty(keywords)) {
        	query1.andLike(LitemallGoods::getKeywords, "%" + keywords + "%");
        	query2.andLike(LitemallGoods::getName, "%" + keywords + "%");
        }
        query2.andEq(LitemallGoods::getIsOnSale, true);
        query1.andEq(LitemallGoods::getIsOnSale, true);
        query2.andEq(LitemallGoods::getDeleted, false);
        query1.andEq(LitemallGoods::getDeleted, false);
        query.or(query1).or(query2);
    	
    	query.orderBy("add_time desc");
    	query.limit(offset, limit);
    	return query.select();
		/*
		 * 
		 * LitemallGoodsExample example = new LitemallGoodsExample();
		 * LitemallGoodsExample.Criteria criteria1 = example.or();
		 * LitemallGoodsExample.Criteria criteria2 = example.or();
		 * 
		 * if (!StringUtils.isEmpty(catId) && catId != 0) {
		 * criteria1.andCategoryIdEqualTo(catId); criteria2.andCategoryIdEqualTo(catId);
		 * } if (!StringUtils.isEmpty(brandId)) { criteria1.andBrandIdEqualTo(brandId);
		 * criteria2.andBrandIdEqualTo(brandId); } if (!StringUtils.isEmpty(isNew)) {
		 * criteria1.andIsNewEqualTo(isNew); criteria2.andIsNewEqualTo(isNew); } if
		 * (!StringUtils.isEmpty(isHot)) { criteria1.andIsHotEqualTo(isHot);
		 * criteria2.andIsHotEqualTo(isHot); } if (!StringUtils.isEmpty(keywords)) {
		 * criteria1.andKeywordsLike("%" + keywords + "%"); criteria2.andNameLike("%" +
		 * keywords + "%"); } criteria1.andIsOnSaleEqualTo(true);
		 * criteria2.andIsOnSaleEqualTo(true); criteria1.andDeletedEqualTo(false);
		 * criteria2.andDeletedEqualTo(false);
		 * 
		 * if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
		 * example.setOrderByClause(sort + " " + order); }
		 * 
		 * PageHelper.startPage(offset, limit);
		 * 
		 * return goodsMapper.selectByExampleSelective(example, columns);
		 */
    }

    public List<LitemallGoods> querySelective(Integer goodsId, String goodsSn, String name, Integer page, Integer size, String sort, String order) {
    	
    	LambdaQuery<LitemallGoods> query = goodsDao.createLambdaQuery();
    	

        if (goodsId != null) {
        	query.andEq(LitemallGoods::getId, goodsId);
        }
        if (!StringUtils.isEmpty(goodsSn)) {
        	query.andEq(LitemallGoods::getGoodsSn, goodsSn);
        }
        if (!StringUtils.isEmpty(name)) {
        	query.andLike(LitemallGoods::getName, "%" + name + "%");
        }
    	query.andEq(LitemallGoods::getDeleted, false);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        	query.orderBy(sort + " " + order);
        }
    	query.orderBy("add_time desc");
    	query.limit(page, size);
    	return query.select();
    	
		/*
		 * LitemallGoodsExample example = new LitemallGoodsExample();
		 * LitemallGoodsExample.Criteria criteria = example.createCriteria();
		 * 
		 * if (goodsId != null) { criteria.andIdEqualTo(goodsId); } if
		 * (!StringUtils.isEmpty(goodsSn)) { criteria.andGoodsSnEqualTo(goodsSn); } if
		 * (!StringUtils.isEmpty(name)) { criteria.andNameLike("%" + name + "%"); }
		 * criteria.andDeletedEqualTo(false);
		 * 
		 * if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
		 * example.setOrderByClause(sort + " " + order); }
		 * 
		 * PageHelper.startPage(page, size); return
		 * goodsMapper.selectByExampleWithBLOBs(example);
		 */
    }

    /**
     * 获取某个商品信息,包含完整信息
     *
     * @param id
     * @return
     */
    public LitemallGoods findById(Integer id) {
    	LitemallGoods entity = new LitemallGoods();
    	entity.setId(id);
    	entity.setDeleted(false);
    	return goodsDao.templateOne(entity);
    	
		/*
		 * LitemallGoodsExample example = new LitemallGoodsExample();
		 * example.or().andIdEqualTo(id).andDeletedEqualTo(false); return
		 * goodsMapper.selectOneByExampleWithBLOBs(example);
		 */
    }

    /**
     * 获取某个商品信息，仅展示相关内容
     *
     * @param id
     * @return
     */
    public LitemallGoods findByIdVO(Integer id) {
    	LitemallGoods entity = new LitemallGoods();
    	entity.setId(id);
    	entity.setIsOnSale(true);
    	entity.setDeleted(false);
    	return goodsDao.templateOne(entity);
		/*
		 * LitemallGoodsExample example = new LitemallGoodsExample();
		 * example.or().andIdEqualTo(id).andIsOnSaleEqualTo(true).andDeletedEqualTo(
		 * false); return goodsMapper.selectOneByExampleSelective(example, columns);
		 */
    }


    /**
     * 获取所有在售物品总数
     *
     * @return
     */
    public Integer queryOnSale() {
    	
    	LitemallGoods entity = new LitemallGoods();
    	entity.setIsOnSale(true);
    	entity.setDeleted(false);
    	return (int)goodsDao.templateCount(entity);
		/*
		 * LitemallGoodsExample example = new LitemallGoodsExample();
		 * example.or().andIsOnSaleEqualTo(true).andDeletedEqualTo(false); return (int)
		 * goodsMapper.countByExample(example);
		 */
    }

    public int updateById(LitemallGoods goods) {
        goods.setUpdateTime(LocalDateTime.now());
        return goodsDao.updateTemplateById(goods);
        //return goodsMapper.updateByPrimaryKeySelective(goods);
    }

    public void deleteById(Integer id) {
    	LitemallGoods entity = new LitemallGoods();
    	entity.setId(id);
    	entity.setDeleted(true);
    	goodsDao.updateTemplateById(entity);
        //goodsMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallGoods goods) {
        goods.setAddTime(LocalDateTime.now());
        goods.setUpdateTime(LocalDateTime.now());
        goodsDao.insertTemplate(goods,true);
        //goodsMapper.insertSelective(goods);
    }

    /**
     * 获取所有物品总数，包括在售的和下架的，但是不包括已删除的商品
     *
     * @return
     */
    public int count() {
    	
    	LitemallGoods entity = new LitemallGoods();
    	entity.setDeleted(false);
    	return (int)goodsDao.templateCount(entity);
    	
		/*
		 * LitemallGoodsExample example = new LitemallGoodsExample();
		 * example.or().andDeletedEqualTo(false);
		 * 
		 * return (int) goodsMapper.countByExample(example);
		 */
    }

    public List<Integer> getCatIds(Integer brandId, String keywords, Boolean isHot, Boolean isNew) {
    	
    	LambdaQuery<LitemallGoods> query = goodsDao.createLambdaQuery();
    	
    	LambdaQuery<LitemallGoods> query1 = goodsDao.createLambdaQuery();
    	
    	LambdaQuery<LitemallGoods> query2 = goodsDao.createLambdaQuery();
    	
    	
        if (!StringUtils.isEmpty(brandId)) {
        	query1.andEq(LitemallGoods::getBrandId, brandId);
        	query2.andEq(LitemallGoods::getBrandId, brandId);
        }
        if (!StringUtils.isEmpty(isNew)) {
        	query1.andEq(LitemallGoods::getIsNew, isNew);
        	query2.andEq(LitemallGoods::getIsNew, isNew);
        	
        }
        if (!StringUtils.isEmpty(isHot)) {
        	query1.andEq(LitemallGoods::getIsHot, isHot);
        	query2.andEq(LitemallGoods::getIsHot, isHot);
        	
        }
        if (!StringUtils.isEmpty(keywords)) {
        	query1.andLike(LitemallGoods::getKeywords, "%" + keywords + "%");
        	query2.andLike(LitemallGoods::getName, "%" + keywords + "%");
        }
        query2.andEq(LitemallGoods::getIsOnSale, true);
        query1.andEq(LitemallGoods::getIsOnSale, true);
        query2.andEq(LitemallGoods::getDeleted, false);
        query1.andEq(LitemallGoods::getDeleted, false);
        query.or(query1).or(query2);
        
        List<LitemallGoods> goodsList = query.select();
        List<Integer> cats = new ArrayList<Integer>();
        for (LitemallGoods goods : goodsList) {
            cats.add(goods.getCategoryId());
        }
        return cats;
    	
    	/*
        LitemallGoodsExample example = new LitemallGoodsExample();
        LitemallGoodsExample.Criteria criteria1 = example.or();
        LitemallGoodsExample.Criteria criteria2 = example.or();

        if (!StringUtils.isEmpty(brandId)) {
            criteria1.andBrandIdEqualTo(brandId);
            criteria2.andBrandIdEqualTo(brandId);
        }
        if (!StringUtils.isEmpty(isNew)) {
            criteria1.andIsNewEqualTo(isNew);
            criteria2.andIsNewEqualTo(isNew);
        }
        if (!StringUtils.isEmpty(isHot)) {
            criteria1.andIsHotEqualTo(isHot);
            criteria2.andIsHotEqualTo(isHot);
        }
        if (!StringUtils.isEmpty(keywords)) {
            criteria1.andKeywordsLike("%" + keywords + "%");
            criteria2.andNameLike("%" + keywords + "%");
        }
        criteria1.andIsOnSaleEqualTo(true);
        criteria2.andIsOnSaleEqualTo(true);
        criteria1.andDeletedEqualTo(false);
        criteria2.andDeletedEqualTo(false);

        List<LitemallGoods> goodsList = goodsMapper.selectByExampleSelective(example, Column.categoryId);
        List<Integer> cats = new ArrayList<Integer>();
        for (LitemallGoods goods : goodsList) {
            cats.add(goods.getCategoryId());
        }
        return cats;*/
    }

    public boolean checkExistByName(String name) {
    	LitemallGoods goods = new LitemallGoods();
    	goods.setName(name);
    	goods.setIsOnSale(true);
    	goods.setDeleted(false);
    	return goodsDao.templateCount(goods)!=0;
		/*
		 * LitemallGoodsExample example = new LitemallGoodsExample();
		 * example.or().andNameEqualTo(name).andIsOnSaleEqualTo(true).andDeletedEqualTo(
		 * false); return goodsMapper.countByExample(example) != 0;
		 */
    }

    public List<LitemallGoods> queryByIds(Integer[] ids) {
    	LambdaQuery<LitemallGoods> query = goodsDao.createLambdaQuery();
    	query.andIn(LitemallGoods::getId, Arrays.asList(ids));
    	query.andEq(LitemallGoods::getDeleted, false);
    	query.andEq(LitemallGoods::getIsOnSale, true);
    	
    	//columns = new Column[]{Column.id, Column.name, Column.brief, Column.picUrl, Column.isHot, Column.isNew, Column.counterPrice, Column.retailPrice};
    	
    	List<LitemallGoods> list = query.select(LitemallGoods::getId , LitemallGoods::getName ,LitemallGoods::getBrief ,LitemallGoods::getPicUrl,LitemallGoods::getIsHot
    			,LitemallGoods::getIsNew,LitemallGoods::getCounterPrice,LitemallGoods::getRetailPrice);
    	return list;
		/*
		 * LitemallGoodsExample example = new LitemallGoodsExample();
		 * example.or().andIdIn(Arrays.asList(ids)).andIsOnSaleEqualTo(true).
		 * andDeletedEqualTo(false); return
		 * goodsMapper.selectByExampleSelective(example, columns);
		 */
    }
}
